//@ sourceURL=studentList.js
var tbStudent = $('#studentlist'),
    fmStudent = $('#fmStudent'),
    selClass = $('#selClass');

$(function() {
    tbStudent.datagrid({
        url: nconStudentList + '/getStudent',
        fit: true,
        fitColumns: true,
        rownumbers: true,
        pagination: true,
        pageSize: 20,
        pageList: [20, 30, 40],
        pagerNumber: 1,
        singleSelect: true,
        columns: [
            [{
                field: 'id',
                title: 'ID',
                // checkbox:true,
                hidden: true
            }, {
                field: 'stuname',
                title: '姓名',
                width: 100
            }, {
                field: 'class',
                title: '班级',
                width: 60
            }, {
                field: 'sex',
                title: '性别',
                width: 60,
                formatter: function(value, rowData, rowIndex) {
                    return value == '0' ? '男' : '女';
                }
            }, {
                field: 'isdel',
                title: '禁用状态',
                width:60,
                formatter:function(value,rowData,rowIndex){
                    return value=='0'?'':'<span style="color:red;">禁用</span>';
                }
            }, {
                field: 'identityid',
                title: '身份证号',
                width: 100
            }, {
                field: 'idfront',
                title: '正面',
                width: 100,
                formatter: function(value, rowData, rowIndex) {
                    return '<img src="' + value + '" style="width:100px;height:100px;"/>';
                }
            }, {
                field: 'idback',
                title: '反面',
                width: 100,
                formatter: function(value, rowData, rowIndex) {
                    return '<img src="' + value + '" style="width:100px;height:100px;"/>';
                }
            }, {
                field: 'phone',
                title: '手机',
                width: 100
            }, {
                field: 'qq',
                title: 'QQ',
                width: 100
            }, {
                field: 'weixin',
                title: '微信',
                width: 100
            }, {
                field: 'weixinid',
                title: '微信ID',
                width: 100
            }, {
                field: 'inserttime',
                title: '操作',
                width: 100,
                formatter: function(value, rowData, rowIndex) {
                    var id = rowData.id;
                    return '<a href="' + nconStudentList + '/printContract/id/' + id + '" target="_blank">打印</a>';
                }
            }]
        ],
        toolbar: '#tbStudent',
    });
});

student = {
    opurl: '',
    add: function() {

        if ($('input[name=idfront]').length == 0) {
            $('#fmStudent input[name=identityid]').after('<p><label>身份证正面：</label><input type="file" name="idfront"></p><p><label>身份证反面：</label><input type="file" name="idback"></p>');
        }
        this.openFm();
        fmStudent.form('clear');
        selClass.combobox('setValue','1704');
        this.opurl = nconStudentList + '/AddStudent';
        $("input:radio[name=sex][value=0]").attr("checked", "true");
    },
    edit: function() {
        $('#fmStudent input[name=idfront]').parent().remove();
        $('#fmStudent input[name=idback]').parent().remove();
        var row = tbStudent.datagrid('getSelected');
        if (row) {
            fmStudent.form('load', row);
            this.openFm();
            this.opurl = nconStudentList + '/UpdateStudent';
        } else {
            $.messager.alert('警告', '请先选择一行要修改的数据！');
        }
    },
    openFm: function() {
        fmStudent.dialog({
            width: 350,
            title: '学员 管理',
            iconCls: 'icon-add',
            modal: true,
            closed: true,
            buttons: [{
                text: '保存',
                iconCls: 'icon-save',
                handler: function() {
                    student.save();
                }
            }, {
                text: '取消',
                iconCls: 'icon-redo',
                handler: function() {
                    fmStudent.dialog('close');
                }
            }]
        }).dialog('open');
    },
    save: function() {
        // $.messager.progress({
        //     text: '正在处理...',
        // });
        var us = fmStudent.form('serialize');
        // console.log(us);
        // return;

        var url = this.opurl;
        console.log(url);

        $("#fmStudent").ajaxSubmit({
            type: 'post',
            url: url,
            success: function(data) {
                console.log(data);
                if (data > 0) {
                    $.messager.show({
                        title: '成功',
                        msg: '保存成功！！！',
                        timeout: 1000,
                        showType: 'slide'
                    });
                    fmStudent.dialog('close');
                    tbStudent.datagrid('reload');
                } else {
                    $.messager.show({
                        title: 'Error',
                        msg: '保存失败！！！'
                    });
                }
            },
            error: function(XmlHttpRequest, textStatus, errorThrown) {
                console.log(XmlHttpRequest);
                console.log(textStatus);
                console.log(errorThrown);
            }
        });


        // $.post(this.opurl, { 'us': JSON.stringify(us) }).then(function(data) {
        //     $.messager.progress('close');
        //     if (data > 0) {
        //         $.messager.show({
        //             title: '成功',
        //             msg: '保存成功！！！',
        //             timeout: 1000,
        //             showType: 'slide'
        //         });
        //         fmStudent.dialog('close');
        //         tbStudent.datagrid('reload');
        //     } else {
        //         $.messager.show({
        //             title: 'Error',
        //             msg: '保存失败！！！'
        //         });
        //     }
        // })
    },

    modifyId: function(fob) {
        var row = tbStudent.datagrid('getSelected');
        if (row) {
            $('#fmIdPhoto').form('load', row);
            $('#fmIdPhoto input[name=fob]').val(fob);
            fob == 'idfront' ? $('#divPhoto').html('<img src="' + row.idfront + '" style="width:335px;"/>') : $('#divPhoto').html('<img src="' + row.idback + '" style="width:335px;"/>');

            $('#fmIdPhoto').dialog({
                width: 350,
                title: '证件照 管理',
                iconCls: 'icon-add',
                modal: true,
                closed: true,
                buttons: [{
                    text: '保存',
                    iconCls: 'icon-save',
                    handler: function() {
                        student.savePhoto();
                    }
                }, {
                    text: '取消',
                    iconCls: 'icon-redo',
                    handler: function() {
                        $('#fmIdPhoto').dialog('close');
                    }
                }]
            }).dialog('open');
        } else {
            $.messager.alert('警告', '请先选择一行要修改的数据！');
        }
    },
    savePhoto: function() {
        $("#fmIdPhoto").ajaxSubmit({
            type: 'post',
            url: nconStudentList + '/savePhoto',
            success: function(data) {
                console.log(data);
                if (data > 0) {
                    $.messager.show({
                        title: '成功',
                        msg: '更换成功！！！',
                        timeout: 1000,
                        showType: 'slide'
                    });
                    $('#fmIdPhoto').dialog('close');
                } else {
                    $.messager.show({
                        title: 'Error',
                        msg: '更换失败！！！'
                    });
                }
            },
            error: function(XmlHttpRequest, textStatus, errorThrown) {
                console.log(XmlHttpRequest);
                console.log(textStatus);
                console.log(errorThrown);
            }
        });
    },

    del: function() {
        var row = tbStudent.datagrid('getSelected');
        if (row) {
            $.messager.confirm('确认框', '确定要删除该条数据吗?', function(r) {
                if (r) {
                    $.post(nconStudentList + '/DeleteStudent/', { id: row.id }, function(data) {
                        if (data > 0) {
                            $.messager.show({
                                title: '删除成功',
                                msg: '删除成功！！！',
                                timeout: 1000,
                                showType: 'slide'
                            });
                            tbStudent.datagrid('reload'); // reload the user data
                        } else {
                            $.messager.show({ // show error message
                                title: 'Error',
                                msg: '删除失败！！！'
                            });
                        }
                    });
                }
            });
        } else {
            $.messager.show({
                title: '提示',
                msg: '还未选择数据，请选择！！！',
                timeout: 1000,
                showType: 'slide'
            });
        }
    },
    abandon:function(){
        var row = tbStudent.datagrid('getSelected');
        if (row) {
            $.messager.confirm('确认框', '确定要禁用该学生吗?', function(r) {
                if (r) {
                    $.post(nconStudentList + '/abandonStudent/', { id: row.id }, function(data) {
                        if (data > 0) {
                            $.messager.show({
                                title: '禁用成功',
                                msg: '禁用成功！！！',
                                timeout: 1000,
                                showType: 'slide'
                            });
                            tbStudent.datagrid('reload'); // reload the user data
                        } else {
                            $.messager.show({ // show error message
                                title: 'Error',
                                msg: '禁用失败！！！'
                            });
                        }
                    });
                }
            });
        } else {
            $.messager.show({
                title: '提示',
                msg: '还未选择数据，请选择！！！',
                timeout: 1000,
                showType: 'slide'
            });
        }
    },
    active:function(){
        var row = tbStudent.datagrid('getSelected');
        if (row) {
            $.messager.confirm('确认框', '确定要启用该学生吗?', function(r) {
                if (r) {
                    $.post(nconStudentList + '/activeStudent/', { id: row.id }, function(data) {
                        if (data > 0) {
                            $.messager.show({
                                title: '启用成功',
                                msg: '启用成功！！！',
                                timeout: 1000,
                                showType: 'slide'
                            });
                            tbStudent.datagrid('reload'); // reload the user data
                        } else {
                            $.messager.show({ // show error message
                                title: 'Error',
                                msg: '启用失败！！！'
                            });
                        }
                    });
                }
            });
        } else {
            $.messager.show({
                title: '提示',
                msg: '还未选择数据，请选择！！！',
                timeout: 1000,
                showType: 'slide'
            });
        }
    },
    search: function() {
        tbStudent.datagrid('load', {
            username: $.trim($('#txtUserName').val())
        });
    },
    reset: function() {
        $('#txtUserName').val('');
        tbStudent.datagrid('load', {});
    },
    resetPwd: function() {
        var row = tbStudent.datagrid('getSelected');
        if (row) {
            $.messager.confirm('确认框', '确定将密码重置为ilovesxj?', function(r) {
                if (r) {
                    $.post(nconStudentList + '/resetPwd/', { id: row.id }, function(data) {
                        if (data > 0) {
                            $.messager.show({
                                title: '重置成功',
                                msg: '重置成功！请提醒员工及时修改密码！',
                                timeout: 3000,
                                showType: 'slide'
                            });
                            tbStudent.datagrid('reload'); // reload the user data
                        } else {
                            $.messager.show({ // show error message
                                title: 'Error',
                                msg: '重置失败！！！'
                            });
                        }
                    });
                }
            });
        } else {
            $.messager.show({
                title: '提示',
                msg: '还未选择数据，请选择！！！',
                timeout: 1000,
                showType: 'slide'
            });
        }
    },
    print: function() {
        var row = tbStudent.datagrid('getSelected');
        if (row) {
            // window.location.href=nconStudentList+'/printContract/id/'+row.id;
            window.open(nconStudentList + '/printContract/id/' + row.id);
        } else {
            $.messager.show({
                title: '提示',
                msg: '还未选择数据，请选择！！！',
                timeout: 1000,
                showType: 'slide'
            });
        }
    }
};


$(document).keydown(function(e) {
    if (e.keyCode == 13) {
        student.search();
    }
});
